<template>
  <div class="container">
    <navBar :title="'隐患详情'"></navBar>
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="item in imgList" :key="item"
        ><img :src="item" alt=""
      /></van-swipe-item>
    </van-swipe>
    <div class="mainBox">
      <div class="title">
        <div>{{ formInfoObj?.dangerName }}</div>
        <div class="status">{{ formInfoObj?.changeStatusName }}</div>
      </div>
      <div class="problemDesc">
        {{ formInfoObj.dangerDescribe }}
      </div>
      <div class="formBox">
        <div class="formItem">
          <van-cell title="隐患名称:">
            <template #default>
              <div>{{ formInfoObj.dangerName }}</div>
            </template>
          </van-cell>
          <van-cell title="隐患来源:">
            <template #default>
              <div>{{ formInfoObj.dangerSourceName }}</div>
            </template>
          </van-cell>
          <van-cell title="隐患类别:">
            <template #default>
              <div>{{ formInfoObj.dangerKindName }}</div>
            </template>
          </van-cell>
        </div>
        <div class="formItem">
          <van-cell title="治理类型:">
            <template #default>
              <div>{{ formInfoObj.governKindName }}</div>
            </template>
          </van-cell>
        </div>
        <div class="formItem">
          <van-cell title="登记人:">
            <template #default>
              <div>{{ formInfoObj.registrantName }}</div>
            </template>
          </van-cell>
          <van-cell title="登记时间:">
            <template #default>
              <div>{{ formInfoObj.createdTime }}</div>
            </template>
          </van-cell>
        </div>

        <div class="formItem">
          <van-cell title="整改负责人:">
            <template #default>
              <div>{{ formInfoObj.changeResponerName || '' }}</div>
            </template>
          </van-cell>
          <div v-if="formInfoObj.changeStatus == 1">
            <van-cell title="整改完成时间:" is-link @click="showPicker = true">
              <template #default>
                {{ changeTime }}
              </template>
            </van-cell>
            <van-cell title="联系方式:">
              <template #default>
                <input
                  type="text"
                  v-model="formInfoObj.changeResponerPhone"
                  class="inputCss"
                  placeholder="请输入"
                />
              </template>
            </van-cell>
            <div class="cellCss">
              <div class="cellCssTitle">关联图片:</div>
              <div>
                <van-uploader
                  v-model="uploader1"
                  :max-count="3"
                  :after-read="afterRead"
                  :before-delete="deleteSubmitPic"
                />
              </div>
            </div>
            <van-cell title="验收负责人:" is-link @click="showPicker4 = true">
              <template #default>
                {{ formInfoObj.solveRespondent }}
              </template>
            </van-cell>
          </div>

          <div v-if="formInfoObj.changeStatus == 2">
            <van-cell title="整改完成时间:">
              <template #default>
                <div>{{ formInfoObj.changeResponerTime }}</div>
              </template>
            </van-cell>
            <van-cell title="联系方式:">
              <template #default>
                <div>{{ formInfoObj.changeResponerPhone }}</div>
              </template>
            </van-cell>
            <div class="cellCss">
              <div class="cellCssTitle">关联图片:</div>
              <div>
                <van-uploader
                  v-model="imgList2"
                  :max-count="3"
                  disabled
                  :before-delete="deleteSubmitPic"
                  :show-upload="false"
                  :deletable="false"
                />
              </div>
            </div>
          </div>
        </div>
        <div class="formItem" v-if="formInfoObj.checkResponerStatus == 1">
          <van-cell title="验收负责人:">
            <template #default>
              <div>{{ formInfoObj.checkResponerName || '' }}</div>
            </template>
          </van-cell>

          <van-cell title="验收意见:">
            <template #default>
              <van-radio-group v-model="ratio" direction="horizontal">
                <van-radio name="2">通过</van-radio>
                <van-radio name="1">未通过</van-radio>
              </van-radio-group>
            </template>
          </van-cell>

          <van-cell title="验收备注:">
            <template #default>
              <input
                type="text"
                v-model="formInfoObj.checkResponerRemark"
                class="inputCss"
                placeholder="请输入"
              />
            </template>
          </van-cell>
          <van-cell title="联系方式:">
            <template #default>
              <input
                type="text"
                v-model="formInfoObj.checkResponerPhone"
                class="inputCss"
                placeholder="请输入"
              />
            </template>
          </van-cell>
        </div>

        <div class="formItem" v-if="formInfoObj.checkResponerStatus == 2">
          <van-cell title="验收负责人:">
            <template #default>
              <div>{{ formInfoObj.checkResponerName || '' }}</div>
            </template>
          </van-cell>

          <van-cell title="验收意见:">
            <template #default>
              <div>{{ formInfoObj.checkResponerRemark || '' }}</div>
            </template>
          </van-cell>
          <van-cell title="验收备注:">
            <template #default>
              <div>{{ formInfoObj.checkResponerPhone || '' }}</div>
            </template>
          </van-cell>
          <van-cell title="联系方式:">
            <template #default>
              <div>{{ formInfoObj.checkResponerTime || '' }}</div>
            </template>
          </van-cell>
        </div>
      </div>
    </div>

    <van-popup v-model="showPicker" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="datetime"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="confirmDate"
        @cancel="cancelShowPicker"
      />
    </van-popup>

    <van-popup v-model="showPicker4" position="bottom">
      <van-picker
        title="整改负责人"
        show-toolbar
        :columns="columns4"
        @confirm="onConfirm4"
        @cancel="showPicker4 = false"
      />
    </van-popup>

    <van-goods-action>
      <van-goods-action-button
        v-if="formInfoObj.changeStatus == 1"
        :style="{
          background: 'linear-gradient(270deg, #00A441  0%, #00C77D 100%)',
        }"
        type="danger"
        text="整改"
        @click="onClickButton"
      />
      <van-goods-action-button
        v-if="formInfoObj.checkResponerStatus == 1"
        :style="{
          background: 'linear-gradient(270deg, #00A441  0%, #00C77D 100%)',
        }"
        type="danger"
        text="验收"
        @click="onClickButton2"
      />
    </van-goods-action>
  </div>
</template>

<script>
import {
  getToDoListApi,
  uploadApi,
  changeHiddenDangerApi,
  listApi,
} from '@/api/index'
import navBar from '@/components/navBar.vue'
export default {
  name: 'problemDetail',
  props: {},
  components: {
    navBar,
  },
  data() {
    return {
      formInfoObj: {
        dangerName: '',
        changeStatusName: '',
        changeResponerName: '',
        dangerDescribe: '',
        dangerSourceName: '',
        dangerKindName: '',
        governKindName: '',
        registrantName: '',
        createdTime: '',
        changeResponerTime: '',
        checkResponerName: '',
        checkResponerRemark: '',
        checkResponerPhone: '',
        checkResponerTime: '',
        solveRespondent: '请选择整改负责人',
        solveRespondentValue: '',
        checkResponerStatus: '',
      },
      imgList: [],
      imgList2: [],
      uploader1: [],
      uploaderSubmit: [],
      minDate: new Date(2024, 0, 1),
      maxDate: new Date(2029, 10, 1),
      changeTime: '请选择时间',
      showPicker: false,
      currentDate: '',
      columns4: [],
      showPicker4: false,
      ratio: '1',
    }
  },
  async created() {
    const res5 = await listApi()
    this.columns4 = res5.data.map((item) => {
      return {
        text: item.name,
        value: item.id,
      }
    })

    this.getToDoList()
  },
  methods: {
    async onClickButton() {
      const res = await changeHiddenDangerApi({
        id: this.formInfoObj.id,
        changeResponerId: this.formInfoObj.changeResponerId,
        changeResponerTime: this.changeTime,
        changeResponerPhone: this.formInfoObj.changeResponerPhone,

        changeResponerImg1: this.uploaderSubmit[0]
          ? this.uploaderSubmit[0]
          : '',
        changeResponerImg2: this.uploaderSubmit[1]
          ? this.uploaderSubmit[1]
          : '',
        changeResponerImg3: this.uploaderSubmit[2]
          ? this.uploaderSubmit[2]
          : '',
        changeStatus: 2,
        checkResponerId: this.formInfoObj.solveRespondentValue,
        checkResponerStatus: 1,
      })
      console.log(res)

      if (res.code === 0) {
        this.$toast.success('整改成功')
        this.$router.back()
      } else {
        this.$toast.fail(res.msg)
      }
    },
    async onClickButton2() {
      const res = await changeHiddenDangerApi({
        id: this.formInfoObj.id,
        checkResponerId: this.formInfoObj.checkResponerId,
        checkResponerRemark: this.formInfoObj.checkResponerRemark,
        checkResponerPhone: this.formInfoObj.checkResponerPhone,
        checkResponerStatus: this.ratio == 1 ? 0 : this.ratio,
        changeStatus: this.ratio == 1 ? 1 : 2,
      })
      console.log(res)

      if (res.code === 0) {
        this.$toast.success('验收成功')
        this.$router.back()
      } else {
        this.$toast.fail(res.msg)
      }
    },
    onConfirm4(value) {
      console.log(value)
      this.formInfoObj.solveRespondent = value.text
      this.formInfoObj.solveRespondentValue = value.value
      this.showPicker4 = false
    },
    async getToDoList() {
      const res = await getToDoListApi({
        id: this.$route.query.id,
      })
      this.formInfoObj = res.data
      this.ratio = String(res.data.checkResponerStatus)
      // 定义前缀
      const keysToExtract = ['dangerImg1', 'dangerImg2', 'dangerImg3']
      // 动态提取所有以特定前缀开头的键值对
      this.imgList = keysToExtract
        .filter(
          (key) =>
            res.data[key] !== null &&
            res.data[key] !== '' &&
            res.data[key] !== undefined
        )
        .map((key) => res.data[key])

      const keysToExtract2 = [
        'changeResponerImg1',
        'changeResponerImg2',
        'changeResponerImg3',
      ]
      // 动态提取所有以特定前缀开头的键值对
      this.imgList2 = keysToExtract2
        .filter(
          (key) =>
            res.data[key] !== null &&
            res.data[key] !== '' &&
            res.data[key] !== undefined
        )
        .map((key) => ({ url: res.data[key] }))
      console.log(this.imgList2)
    },
    async afterRead(file) {
      // 此时可以自行将文件上传至服务器
      const formData = new FormData()
      formData.append('file', file.file)
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0,
      })
      const res = await uploadApi(formData)
      // console.log(1);
      if (res.code === 0) {
        this.uploaderSubmit.push(res.data)
      } else {
        this.uploader1.pop()
        console.log(this.uploader1)
      }
      this.$toast.clear()

      setTimeout(() => {
        this.$toast.clear()
      }, 3000)
    },
    deleteSubmitPic(e, detail) {
      this.uploaderSubmit.splice(detail.index, 1)
      this.uploader1.splice(detail.index, 1)
    },
    confirmDate(e) {
      this.changeTime = this.formatDate(e)
      this.showPicker = false
    },
    cancelShowPicker() {
      this.showPicker = false
      this.changeTime = '请选择时间'
      this.changeTime = ''
    },
    formatDate(date) {
      // console.log(date)
      // 获取年、月、日、时、分、秒
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0') // 月份从0开始，需要加1
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0') // 获取小时
      const minutes = String(date.getMinutes()).padStart(2, '0') // 获取分钟
      const seconds = String(date.getSeconds()).padStart(2, '0') // 获取秒
      // 拼接成目标格式
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    },
  },
  mounted() {},
  watch: {},
  computed: {},
  filters: {},
}
</script>

<style scoped lang="scss">
.my-swipe {
  height: 200px;
  .van-swipe-item {
    color: #fff;
    font-size: 20px;
    line-height: 150px;
    text-align: center;
    background-color: transparent;
  }
  ::v-deep .van-swipe__indicators {
    bottom: 32px;
  }
}
.mainBox {
  background: #f7f7f7;
  border-radius: 16px 16px 0 0;
  padding: 16px;
  transform: translateY(-20px);
  box-sizing: border-box;
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 18px;
    font-weight: bold;
    color: #2f3331;
    margin-bottom: 12px;
    .status {
      width: 52px;
      height: 24px;
      background: #00b9c7;
      border-radius: 4px;
      font-size: 12px;
      color: #fff;
      text-align: center;
      line-height: 24px;
    }
  }
  .problemDesc {
    // width: 343px;
    height: 80px;
    background: #ffffff;
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 10px;
    // box-sizing: border-box;
  }
}
.formBox {
  // padding: 16px;
  background: #f7f7f7;
  padding-bottom: 60px;
  .formTitle {
    font-weight: bold;
    font-size: 16px;
    color: #2f3133;
    margin-bottom: 16px;
  }
  .formItem {
    border-radius: 10px; /* 设置圆角大小 */
    overflow: hidden; /* 隐藏超出部分 */
    background-color: #fff; /* 可选：设置背景颜色 */
    margin-bottom: 12px;
    .inputCss {
      border: none;
      outline: none;
      background: transparent;
      font-size: 14px;
      text-align: right; /* 添加这行代码 */
    }
    .cellCss {
      box-sizing: border-box;
      width: 100%;
      padding: 10px 16px;
      overflow: hidden;
      color: #323233;
      font-size: 14px;
      line-height: 24px;
      background-color: #fff;
      .cellCssTitle {
        margin-bottom: 10px;
      }
      textarea {
        width: 100%;
        border: none;
      }
    }
  }
}
.van-cell {
  align-items: center;
  ::v-deep .van-field__label {
    color: #323233 !important ;
  }
}
.van-cell__value {
  overflow: visible;
}
.van-radio-group--horizontal {
  float: right;
}
</style>
